export function obj2datetime(time) {
    /*
    * 时间对象转时间
    */
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}

export function obj2date(time) {
    /*
    * 时间对象转日期
    */
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    return y + '-' + add0(m) + '-' + add0(d);
}

export function str2date(str) {
    /*
    * 时间日期转时间对象
    */
    str = str.replace(/-/g, '/'); // 将-替换成/，因为下面这个构造函数只支持/分隔的日期字符串
    let _date = new Date(str); // 构造一个日期型数据，值为传入的字符串
    return _date
}

function add0(m) {
    return m < 10 ? '0' + m : m
}

export function formatTimestamp(timestamp) {
    /*
    格式化时间戳
     */
    if (timestamp === undefined || timestamp === '-' || timestamp === '') return '-'
    var time = new Date(timestamp * 1000);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}


export function timeStr(time) {
    time = Number(time)
    let t = 0
    let m = ''
    if (time < 60) {
        t = time
        m = '秒'
    } else if (time < 60 * 60) {
        t = (time / 60).toFixed(2)
        m = '分'
    } else {
        t = (time / 60 / 60).toFixed(2)
        m = '小时'
    }
    return String(t) + m
}
